package com.example.emos.wx.config;

import cn.hutool.core.util.StrUtil;
import com.example.emos.wx.db.mapper.SysConfigMapper;
import com.example.emos.wx.db.pojo.SysConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.lang.reflect.Field;
import java.util.List;

/**
 * @ClassName SystemConstantsConfig
 * @Description TODO
 * @Author daoketa
 * @Version 1.0
 * 系统考勤时间变量初始化
 * 图片文件夹创建
 **/
@Slf4j
@Configuration
public class SystemConstantsConfig {
    @Autowired
    private SysConfigMapper sysConfigMapper;

    @Autowired
    private SystemConstants  systemConstants;

    @Value("${emos.image-folder}")
    private String imageFolder;

    @PostConstruct
    public void init(){
        List<SysConfig> list = sysConfigMapper.selectAllParam();
        list.forEach(one -> {
            String paramKey = one.getParamKey();
            paramKey = StrUtil.toCamelCase(paramKey);
            try {
                Field field = systemConstants.getClass().getDeclaredField(paramKey);
                field.set(systemConstants, one.getParamValue());
            } catch (NoSuchFieldException e) {
                log.error( "执行异常", e );
            } catch (IllegalAccessException e) {
                log.error( "执行异常", e );
            }
        });
        /** 项目启动时候自动创建图片文件夹 */
        new File(imageFolder).mkdirs();
    }

}
